package com.cz.run.web;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.cz.run.beans.base.PageDTO;
import com.cz.run.beans.base.ReturnDTO;
import com.cz.run.beans.mysql.UserPocketLog;
import com.cz.run.beans.po.pocket.ListPO;
import com.cz.run.beans.vo.pocket.PocketLogVO;
import com.cz.run.service.IUserPocketLogService;
import com.cz.run.utils.DateUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 口袋日志表 前端控制器
 * </p>
 *
 * @author CAIYJ
 * @since 2018-06-02
 */
@RestController
@RequestMapping("/userPocketLog")
public class UserPocketLogServer {

    @Autowired
    private IUserPocketLogService iUserPocketLogService;

    @ApiOperation(value = "口袋交易记录列表", notes = "口袋交易记录列表", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @RequestMapping(value = "/list")
    public ReturnDTO<PageDTO<UserPocketLog>> list(@RequestBody ListPO po) throws Exception {
        Page<UserPocketLog> page = new Page<>(po.getCurrentPage(), po.getPageSize());
        EntityWrapper<UserPocketLog> ew = new EntityWrapper<>();
            ew.where(!StringUtils.isEmpty(po.getAddress()), "address = {0}", po.getAddress());

        page = iUserPocketLogService.selectPage(page, ew);
        if (page != null && page.getRecords() != null && page.getRecords().size() > 0) {
            List<PocketLogVO> list = new ArrayList<>();
            PocketLogVO vo = null;
            for (UserPocketLog userPocketLog : page.getRecords()) {
                vo = new PocketLogVO();
                vo.setAmount(userPocketLog.getAmount());
                vo.setFrom(userPocketLog.getFrom());
                vo.setId(userPocketLog.getId());
                vo.setType(userPocketLog.getType());
                if (null != userPocketLog.getCreateTime()) {
                    vo.setTime(DateUtil.currentTimeMillisCN3(userPocketLog.getCreateTime())+"\n"+DateUtil.currentTime2(userPocketLog.getCreateTime()));
                }
                list.add(vo);
            }
            po.setContent(list);
        }
        po.setTotalRecord(page.getTotal());
        return ReturnDTO.success(po);
    }

}

